package org.whb.org.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.whb.basic.util.AjaxResult;
import org.whb.basic.util.PageList;
import org.whb.org.domain.SystemDictionaryType;
import org.whb.org.query.SystemDictionaryTypeQuery;
import org.whb.org.service.ISystemDictionaryTypeService;

import java.util.List;

@RestController
@RequestMapping("/dictype")
public class SystemDictionaryTypeController {

    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;


    @PostMapping("/list")
    public PageList<SystemDictionaryType> queryPage(@RequestBody SystemDictionaryTypeQuery query){
        return systemDictionaryTypeService.queryPage(query);
    }

    @PutMapping
    public AjaxResult addorSystemDictionaryType(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if(systemDictionaryType.getId()==null){//表示新增
                systemDictionaryTypeService.add(systemDictionaryType);
            }else {//表示修改
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("操作失败");
        }
    }

    @DeleteMapping("{id}")
    public AjaxResult removeDept(@PathVariable Long id){
        try {
            systemDictionaryTypeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids 批量删除的id
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            systemDictionaryTypeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("删除失败");
        }
    }

    @GetMapping
    public List<SystemDictionaryType> queryAll(){
        return systemDictionaryTypeService.queryAll();
    }
}
